package com.woniu.user.service.impl;

import com.woniu.entity.Result;
import com.woniu.user.DTO.ArticlePublishDTO;
import com.woniu.user.mapper.PictureMapper;
import com.woniu.user.pojo.Article;
import com.woniu.user.mapper.ArticleMapper;
import com.woniu.user.pojo.Picture;
import com.woniu.user.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.user.util.TencentOSS;
import com.woniu.util.IdWorker;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author He
 * @since 2023-02-17
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private PictureMapper pictureMapper;



    @Override
    public Result publish(ArticlePublishDTO articlePublishDTO,MultipartFile[] multipartFiles) {
        if (multipartFiles.length > 9){
            return Result.error(401,"超过9张无法上传");
        }
        long nextId = new IdWorker().nextId();
        System.out.println(nextId);
        for (MultipartFile multipartFile : multipartFiles) {
            Picture picture = new Picture();//图片资源对象
            String uuid = UUID.randomUUID().toString();
            String img = TencentOSS.UploadIMG(multipartFile,uuid);//返回图片MD5
            picture.setPicture(uuid);
            picture.setImgId(String.valueOf(nextId));
            pictureMapper.insert(picture);
        }

        Article article = new Article();//文章
        article.setUserId(articlePublishDTO.getUserId());
        article.setCarCircleId(articlePublishDTO.getCarCircleId());
        article.setTitle(articlePublishDTO.getTitle());
        article.setContent(articlePublishDTO.getContent());
        article.setImgId(String.valueOf(nextId));


        int articleInsert = articleMapper.insert(article);
        if (articleInsert < 1){
            return Result.error(401,"错误");
        }

        return Result.ok(200);
    }

    @Override
    public Result getListArticle() {
//        articleMapper.selectPage()
        return null;
    }

}
